﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Timetable.Base.Entities;
using Timetable.Site.Models;
using Timetable.Site.Models.Schedule;
using Timetable.Base.Interfaces.Services.DataServices.Extensions;

namespace Timetable.Site.Controllers.Api
{
    public class ScheduleController : BaseApiController<Schedule>
    {
        /// <summary>
        /// Возвращает расписание для выбранной группы
        /// </summary>
        /// <param name="groupid">Идентификатор группы</param>
        /// <returns></returns>
        public HttpResponseMessage GetForGroup(int groupid)
        {
            return CreateResponse<int, IEnumerable<Schedule>>(privateGetForGroup, groupid);
        }
        private IEnumerable<Schedule> privateGetForGroup(int groupId)
        {
            var result = DataService.GetByGroupId(groupId);

            return result;
        }

        /// <summary>
        /// Возвращает расписание для выбранной аудитории
        /// </summary>
        /// <param name="auditoriumId">Идетификатор аудитории</param>
        /// <returns></returns>
        public HttpResponseMessage GetForAuditorium(int auditoriumId)
        {
            return CreateResponse<int, IEnumerable<Schedule>>(privateGetForAuditorium, auditoriumId);
        }
        private IEnumerable<Schedule> privateGetForAuditorium(int auditoriumId)
        {
            return DataService.GetByAuditoriumId(auditoriumId);
        }

        /// <summary>
        /// Возвращает расписание по идетификатору
        /// </summary>
        /// <param name="id">Идетификатор расписания</param>
        /// <returns></returns>
        public HttpResponseMessage GetById(int id)
        {
            return CreateResponse<int, Schedule>(privateGetById, id);
        }
        private Schedule privateGetById(int id)
        {
            return DataService.GetById(id);
        }

        /// <summary>
        /// Добавить новую запись в расписание
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Add(AddModel model)
        {
            return CreateResponse(privateAdd, model);
        }
        private void privateAdd(AddModel model)
        {
            //var auditorium = DataService.GetAuditoriumByNumber(model.Auditorium, 1);
            //var lecturer = DataService.GetLecturerByFirstMiddleLastname(model.Lecturer);
            //var group = DataService.GetGroup(model.Group.Id);
            //var tutorial = DataService.GetTutorial(model.Tutorial.Id);
            //var tutorialType = DataService.GetTutorialType(model.TutorialType.Id);

            //foreach (var time in model.Times)
            //{
            //    var period = DataService.GetPeriod(time.Id);

            //    foreach (var dayOfWeek in model.DaysOfWeek)
            //    {
            //        var schedule = new Schedule
            //        {
            //            Auditorium = auditorium,
            //            Period = period,
            //            DayOfWeek = dayOfWeek
            //        };
            //        DataService.Add(schedule);
            //    }
            //}
        }

        /// <summary>
        /// Обновить выбранную запись в расписании
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Update(UpdateModel model)
        {
            return CreateResponse(privateUpdate, model);
        }
        private void privateUpdate(UpdateModel model)
        {
            //var auditorium = DataService.GetAuditoriumByNumber(model.Auditorium, 1);
            //var lecturer = DataService.GetLecturerByFirstMiddleLastname(model.Lecturer);
            //var group = DataService.GetGroup(model.Group.Id);
            //var tutorial = DataService.GetTutorial(model.Tutorial.Id);
            //var tutorialType = DataService.GetTutorialType(model.TutorialType.Id);
            //var period = DataService.GetPeriod(model.Time.Id);

            //var schedule = DataService.GetScheduleById(model.Id);
            //schedule.Auditorium = auditorium;
            //schedule.Period = period;
            //schedule.DayOfWeek = model.DaysOfWeek;

            //DataService.UpdateSchedule(schedule);
            throw new NotImplementedException();
        }

        /// <summary>
        /// Удалить запись из расписания
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Delete(DeleteModel model)
        {
            return CreateResponse(privateDelete, model.Id);
        }
        private void privateDelete(int id)
        {
            var schedule = DataService.GetById(id);
            DataService.Delete(schedule);
        }
    }
}